package day_2022_9_to_12.Day;

import java.util.HashSet;
import java.util.Set;

/**
 * @author haomin
 * @date 2022/10/12 15:44
 **/
public class Day40_1_Num817_NumComponents {
    class Solution {
        public int numComponents(ListNode head, int[] nums) {
            int ans = 0;
            Set<Integer> set = new HashSet<>();
            for(int i : nums) set.add(i);
            boolean pre = false;
            ListNode cur = head;
            while(cur != null){
                if(set.contains(cur.val)){
                    if(!pre) {
                        ans++;
                        pre = true;
                    }
                }else {
                    pre = false;
                }
                cur = cur.next;
            }
            return ans;
        }
    }
}